﻿using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NNS.Framework;
using NNS.Utils.Security2;
using QLTB.Sercurity.Entity;

namespace QLTB.Sercurity.UI
{
    public partial class frmDBOption : XtraForm
    {
        public bool FirstRun = true;


        private string pw = "@#$%^%123THgff678";

        public frmDBOption()
        {
            InitializeComponent();
            Read();
        }

        public event EventHandler SaveChanged;

        private void OnSaveChanged(EventArgs e)
        {
            EventHandler _handler = SaveChanged;
            if (_handler != null) _handler(this, e);
        }

        public bool Read()
        {
            if (File.Exists(Application.StartupPath + "\\Info.sys"))
            {
                var ds = new dsLogin();
                ds.DBOption.ReadXml(Application.StartupPath + "\\Info.sys");
                if (ds.DBOption.Count > 0)
                {
                    txtmaychu.Text = MyEncryption.Decrypt(ds.DBOption.Rows[0][ds.DBOption.ServerColumn] as string, pw,
                                                          true);
                    txtuser.Text = MyEncryption.Decrypt(ds.DBOption.Rows[0][ds.DBOption.UserColumn] as string, pw, true);
                    txtpass.Text = MyEncryption.Decrypt(ds.DBOption.Rows[0][ds.DBOption.PassColumn] as string, pw, true);
                    txtCSDL.Text = MyEncryption.Decrypt(ds.DBOption.Rows[0][ds.DBOption.CSDLColumn] as string, pw, true);
                    DbHelper.ConnectionString =
                        MyEncryption.Decrypt(ds.DBOption.Rows[0][ds.DBOption.ConnectionStringColumn] as string, pw, true);

                    return true;
                }
            }

            return false;
        }

        public void Save()
        {
            var ds = new dsLogin();

            ds.DBOption.AddDBOptionRow(
                MyEncryption.Encrypt(txtmaychu.Text, pw, true),
                MyEncryption.Encrypt(txtuser.Text, pw, true),
                MyEncryption.Encrypt(txtpass.Text, pw, true),
                MyEncryption.Encrypt(txtCSDL.Text, pw, true),
                MyEncryption.Encrypt(DbHelper.ConnectionString, pw, true));

            ds.DBOption.WriteXml(Application.StartupPath + "\\Info.sys");
            OnSaveChanged(null);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            string connect = SqlHelper.RebuildConnectionString(txtmaychu.Text, txtCSDL.Text, txtuser.Text,
                                                               txtpass.Text, false, 30); //, pw, true)
            if (!SqlHelperSmart.Check(connect))
            {
                MyBox.ShowWarning("Không thể kết nối đến máy chủ!\nVui lòng kiểm tra lại!", "Quản lý thiết bị");
                return;
            }
            DbHelper.ConnectionString = connect;
            FirstRun = false;
            Save();
            Cursor.Current = current;
            Close();
        }

        private void frmDBOption_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FirstRun) Environment.Exit(0);
            Hide();
            e.Cancel = !Program._exit;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            btnCheck.Enabled = false;
            string connect = SqlHelper.RebuildConnectionString(txtmaychu.Text, txtCSDL.Text, txtuser.Text,
                                                               txtpass.Text, false, 30); //, pw, true)
            if (!SqlHelperSmart.Check(connect))
            {
                MyBox.ShowWarning("Không thể kết nối đến máy chủ!\nVui lòng kiểm tra lại!", "Quản lý thiết bị");
                return;
            }
            btnCheck.Enabled = true;
            Cursor.Current = current;
        }
    }
}